package MyPro;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy {
    public static ProjectInterface create(ProjectInterface projectInterface){

        ProjectInterface myProjectProxy = (ProjectInterface) Proxy.newProxyInstance(MyProxy.class.getClassLoader(),
                new Class[]{ProjectInterface.class}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        if (method.getName().equals("login") || method.getName().equals("delete") || method.getName().equals("searching")){

                            long startTime = System.currentTimeMillis();

                            Object obj = method.invoke(projectInterface,args);

                            long endTime = System.currentTimeMillis();
                            System.out.println( method.getName() + "花费了时间为: " + (endTime-startTime)/1000 + " 秒");
                            return obj;
                        }else {
                            Object obj = method.invoke(projectInterface,args);
                            return obj;
                        }
                    }
                }
        );

        return myProjectProxy;

    }
}
